import {createRouter, createWebHistory} from 'vue-router'
import {getLoginUser} from "@/api/httpApi/userApi.js";
import {successCode, timeoutUrl} from "@/api/httpApi/base/http.js";
import {userStore} from "@/stores/userStore.js";
import {adminUrl, portalUrl, ROLE_ADMIN, ROLE_MASTER, ROLE_ROOT} from "@/api/const.js";
/**
 * 路由界面
 */
import baseView from "@/views/layout/base.vue";
import {constantStore} from "@/stores/constantStore.js";


let routes = [
    {
        path: '/',
        redirect: '/index',
        component: baseView,
        children: [
            {
                path: "/index",
                name: 'index',
                alias: '首页',
                icon: 'icon-shouye',
                power: ROLE_ADMIN,
                // meta:{
                //   title:"首页"
                // },
                component: () => import("@/views/index.vue")
            },
            {
                path: "/user",
                name: 'user',
                alias: '用户管理',
                icon: 'icon-yonghuguanli',
                power: ROLE_ADMIN,
                component: () => import("@/views/user/userinfo.vue")
            },
            {
                path: "/order",
                name: 'order',
                alias: '订单管理',
                icon: 'icon-shouye',
                power: ROLE_ADMIN,
                component: () => import("@/views/order/orderInfo.vue")
            },
            {
                path: "/commodity",
                name: 'commodity',
                alias: '商品管理',
                icon: 'icon-shouye',
                power: ROLE_MASTER,
                component: () => import("@/views/commodity/commodity.vue")
            },
            {
                path: '/group',
                name: 'group',
                alias: '分类管理',
                icon: 'icon-shouye',
                power: ROLE_MASTER,
                children: [
                    {
                        path: "category",
                        name: 'category',
                        alias: '商品分类管理',
                        icon: 'icon-shouye',
                        power: ROLE_MASTER,
                        component: () => import("@/views/category/CommodityCategory.vue")
                    },
                    {
                        path: "project",
                        name: 'project',
                        alias: '商城专栏管理',
                        icon: 'icon-shouye',
                        power: ROLE_MASTER,
                        component: () => import("@/views/category/ShopType.vue")
                    }
                ]
            },
            {
                path: '/address',
                name: "address",
                alias: '地址管理',
                icon: 'icon-shouye',
                power: ROLE_ROOT,
                children: [
                    {
                        path: "address-map",
                        name: 'map',
                        alias: '方舟地图',
                        icon: 'icon-shouye',
                        power: ROLE_ROOT,
                        component: () => import("@/views/address/map.vue"),
                    },
                ]
            },
            {
                path: "/advertisement",
                name: 'advertisement',
                alias: '广告管理',
                icon: 'icon-guanggaoguanli',
                power: ROLE_ROOT,
                component: () => import("@/views/index.vue")
            },
            {
                path: "/web",
                name: 'web',
                alias: '网站管理',
                icon: 'icon-wangzhanxinxi',
                power: ROLE_ROOT,
                component: () => import("@/views/web/webInfo.vue")
            },
            {
                path: "/terminal",
                name: 'terminal',
                alias: '终端管理',
                icon: 'icon-wangzhanxinxi',
                power: ROLE_ROOT,
                component: () => import("@/views/terminal/terminal.vue")
            },
            {
                path: "/carmine",
                name: 'carmine',
                alias: '卡密管理',
                icon: 'icon-wangzhanxinxi',
                power: ROLE_ROOT,
                component: () => import("@/views/terminal/carmine.vue")
            },
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/login/login2.vue'),
        meta: {title: '登录'}
    },
    {
        path: timeoutUrl,
        name: 'timeout',
        component: () => import('@/views/error/timeout.vue'),
        meta: {title: '登录'}
    },
    {
        path: '/404',
        name: 'NoPage404',
        component: () => import('@/views/error/notFound.vue'),
        hidden: true
    },
    {
        path: '/:pathMatch(.*)',
        redirect: '/404',
        hidden: true
    }
]

const router = createRouter({
    history: createWebHistory(),
    mode: 'hash',
    routes: routes
})


// 全局守卫
router.beforeEach(async (to, from, next) => {
    document.title = constantStore().webTitle + "管理界面"
    let isUser = userStore().user;
    if (!isUser) {
        let loginUser = await getLoginUser();
        if (successCode === loginUser.code) {
            userStore().setLoginUser(loginUser.data)
        }
        isUser = userStore().user;
    }
    let admin = userStore().admin;
    //判断是否有权限进入界面
    if (isUser && !admin) {
        // location.href = portalUrl;
        next();
    }
    if (to.path === "/login") {
        if (isUser) {
            next({path: '/index'});
        }
        location.href = portalUrl + "/login?redirect=login";
        next();
    } else if (to.path === timeoutUrl || to.path === "/404") {
        next();
    } else {
        if (!isUser) {
            location.href = portalUrl + "/login?redirect=login";
            next("/login")
        }
        next();
    }

})

export default router
